package com.it.base.core.system.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.it.base.common.view.PageResult;
import com.it.base.common.view.Result;
import com.it.base.common.view.ResultCode;
import com.it.base.core.system.user.model.covert.SysUserCovert;
import com.it.base.core.system.user.model.dto.SysUserDTO;
import com.it.base.core.system.user.model.dto.SysUserListDTO;
import com.it.base.core.system.user.model.entity.SysUser;
import com.it.base.core.system.user.model.from.SysUserDeleteOneFormDTO;
import com.it.base.core.system.user.model.from.SysUserSaveFormDTO;
import com.it.base.core.system.user.model.from.SysUserSelectOneFormDTO;
import com.it.base.core.system.user.model.from.SysUserSelectPageListFormDTO;
import com.it.base.core.system.user.model.from.SysUserUpdateFormDTO;
import com.it.base.core.system.user.model.from.SysUserValidatorFormDTO;
import com.it.base.core.system.user.service.ISysUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("system/user")
@ApiSort(value = 4)
@Api(tags = "4.用户数据")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("test")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "1.测试", notes = "测试")
    public Result<?> test() {
        return Result.SUCCESS();
    }

    @PostMapping("selectPageList")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "2.分页查询用户数据", notes = "分页用户")
    public Result<PageResult<SysUserListDTO>> selectPageList(
            @Validated @RequestBody SysUserSelectPageListFormDTO sysDeptSelectPageListFormDTO) {
        PageResult<SysUser> page = sysUserService.selectPageList(sysDeptSelectPageListFormDTO);
        List<SysUserListDTO> datas = SysUserCovert.INSTANCE.entity2DTOList(page.getRows());
        return Result.SUCCESS(new PageResult<SysUserListDTO>(page.getTotal(), datas));
    }

    @PostMapping("validatorExist")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "3.校验用户数据", notes = "校验用户数据")
    public Result<?> validatorExist(@Validated @RequestBody SysUserValidatorFormDTO sysUserValidatorFormDTO) {
        sysUserValidatorFormDTO.setId(null);
        return Result.SUCCESS(this.sysUserService.validatorExist(sysUserValidatorFormDTO));
    }

    @PostMapping("saveOne")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "4.新增用户数据", notes = "新增用户数据")
    public Result<String> saveOne(@Validated @RequestBody SysUserSaveFormDTO sysUserSaveFormDTO) {
        // 1.校验
        SysUserValidatorFormDTO sysUserValidatorFormDTO = SysUserValidatorFormDTO.builder()
                //
                .userName(sysUserSaveFormDTO.getUserName())//
                .password(sysUserSaveFormDTO.getPassword())//
                .build();
        boolean flag = this.sysUserService.validatorExist(sysUserValidatorFormDTO);
        if (flag) {
            return new Result<>(ResultCode.E_100116);
        }

        // 2.保存
        SysUser sysUser = SysUserCovert.INSTANCE.from2Entity(sysUserSaveFormDTO);
        this.sysUserService.save(sysUser);
        return Result.SUCCESS(sysUser.getId());
    }

    @PutMapping("updateOne")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "5.更新用户数据", notes = "更新用户数据")
    public Result<?> updateOne(@Validated @RequestBody SysUserUpdateFormDTO sysUserUpdateFormDTO) {
        // 1.校验
        SysUserValidatorFormDTO sysUserValidatorFormDTO = SysUserValidatorFormDTO.builder()//
                .userName(sysUserUpdateFormDTO.getUserName())
                .id(sysUserUpdateFormDTO.getId())//
                .build();
        boolean flag = this.sysUserService.validatorExist(sysUserValidatorFormDTO);
        if (!flag) {
            return new Result<>(ResultCode.E_1001128);
        }

        // 3.判断部门状态不能停用

        // 4.更新部门
        // 4.1修改自身
        // 4.2修改子元素

        SysUser sysUser = SysUserCovert.INSTANCE.from2Entity(sysUserUpdateFormDTO);
        this.sysUserService.updateById(sysUser);
        return Result.SUCCESS(sysUser.getId());
    }

    @DeleteMapping("deleteOne")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "6.删除用户", notes = "删除用户")
    public Result<Object> deleteOne(@Validated @RequestBody SysUserDeleteOneFormDTO sysUserDeleteOneFormDTO) {
        this.sysUserService.removeById(sysUserDeleteOneFormDTO.getId());
        return Result.SUCCESS();
    }

    @PostMapping("selectOne")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "7.查询用户数据", notes = "查询用户数据")
    public Result<SysUserDTO> selectOne(@Validated @RequestBody SysUserSelectOneFormDTO sysUserSelectOneFormDTO) {
        SysUser sysUser = this.sysUserService.getById(sysUserSelectOneFormDTO.getId());
        SysUserDTO sysUserDTO = SysUserCovert.INSTANCE.entity2DTO(sysUser);
        return Result.SUCCESS(sysUserDTO);
    }

}
